"""
@name : 8.装饰类
@author : wenyao
@projectname: sanchuanglianxi
"""
#按照创建时间 去给实例化的对象排序

import functools
import time

#普通类实现
# class Sortable():
#     def __init__(self, identifier):
#         self.identifier = identifier
#         self.creat_time = time.time()
#
#     def __repr__(self):
#         return self.identifier
#
#     def __lt__(self,other):
#         if self.creat_time < other.creat_time:
#             return True
#         else:
#             return False
# yy = Sortable("yy")
# time.sleep(0.1)
# first = Sortable("first")
# time.sleep(0.1)
# second = Sortable("second")
# time.sleep(0.1)
# third = Sortable("third")
#
# result = [first, second, third,yy]
# print(sorted(result))

#装饰器实现
# def sortable_by_create_time(cls):
#     original_init = cls.__init__
#
#     #给init函数添加self.creat_time属性
#     @functools.wraps(original_init)
#     def new_init(self, *args, **kwargs):
#         original_init(self, *args, **kwargs)
#         self.creat_time = time.time()
#
#     cls.__init__ = new_init
#     #定义"<"的行为
#     def lt(self,other):
#         if self.creat_time < other.creat_time:
#             return True
#         else:
#             return False
#     cls.__lt__ = lt
#     return cls

#@sortable_by_create_time
# class Sortable():
#     def __init__(self, identifier):
#         self.identifier = identifier
#
#     def __repr__(self):
#         return self.identifier
#
# yy = Sortable("yy")
# time.sleep(0.1)
# first = Sortable("first")
# time.sleep(0.1)
# second = Sortable("second")
# time.sleep(0.1)
# third = Sortable("third")
#
# result = [first, second, third,yy]
# print(sorted(result))

# @sortable_by_create_time
# class A:
#     pass
#
#
# a = A()
# time.sleep(0.1)
# b = A()
# time.sleep(0.1)
# c = A()
# print(sorted([c,b,a]))


def outer(cls):
    def inner(a):
        print("class name:",cls.__name__)
        return cls(a)
    return inner
@outer
class A():
    def __init__(self,a):
        self.a = a
m = A("test")
